package com.zhang.exception;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;

@Order(value = Ordered.HIGHEST_PRECEDENCE)//设置为最高优先级
@Component
public class CustomHandlerExceptionResolver implements HandlerExceptionResolver {


    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {

        ModelAndView modelAndView = new ModelAndView();
        if(e instanceof ClassCastException){
            modelAndView.addObject("info","类型转换异常");
        }else if(e instanceof FileNotFoundException){
            modelAndView.addObject("info","文件未找到异常");
        }

        modelAndView.setViewName("error1");

        return modelAndView;
    }
}









